#!/usr/bin/env python
# -*- coding: utf-8 -*-

def singleton(cls):
    instances={}
    #bug cls __init__.py=>core.Core
    #Druid.py=>druid.core.Core
    def getinstance(*arg1,**arg2):
        if arg2.has_key("delete") and arg2["delete"]:
            del instances[cls]
            return None
        if cls not in instances:
            instances[cls] = cls(*arg1)
        return instances[cls]
    return getinstance


def multiton(cls):
    instances = {}
    def getinstance(*arg1,**arg2):
        if not arg2.has_key("name"):
            name="DEFAULT"+str(cls)
        else:
            name=arg2["name"]+str(cls)
        if arg2.has_key("delete") and arg2["delete"]:
            del instances[cls]
            return None
        if cls not in instances:
            instances[name] = cls(*arg1)
        return instances[name]
    return getinstance


class Multiton (object):
    __instance = {}       # Attribut statique de classe

    def __new__(self, name, *arg): 
        if not self.__instance.has_key(name):
            self.__instance[name] = object.__new__(self, *arg)
            if hasattr(self, '__single_init__'):
                self.__instance.__single_init__()
        return self.__instance
    
    
    @classmethod
    def getInstance(self,name):
        if not self.__instance.has_key(name):
            self.__instance[name] = object.__new__(self)
        return self.__instance
    
    @classmethod
    def destroy(self, instance):
        self.__instance=None
        del self._instance
        
        

class Singleton (object):
    __instance = None       # Attribut statique de classe

    def __new__(self, *arg): 
        if not self.__instance:
            self.__instance = object.__new__(self, *arg)
            if hasattr(self, '__single_init__'):
                self.__instance.__single_init__()
        return self.__instance
    
    
    @classmethod
    def getInstance(self):
        if(self.__instance is None):
            self.__instance = object.__new__(self)
        return self.__instance
    
    def destroy(self):
        self.__instance=None
        del self.__instance
